// subpackage_volleyball/training/training.ts

// 训练日程数据接口
export interface TrainingSchedule {
  id: string
  date: string
  weekday: string
  title: string
  startTime: string
  endTime: string
  location: string
  coach: string
  participants: number
  status: 'upcoming' | 'completed'
  description?: string
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    trainingSchedules: [] as TrainingSchedule[], // 训练日程列表
    loading: true, // 加载状态
    hasMore: true, // 是否有更多数据
    pageNum: 1, // 当前页码
    pageSize: 10, // 每页数据量
    upcomingCount: 0, // 即将开始的训练数量
    completedCount: 0, // 已完成的训练数量
    totalCount: 0, // 总训练数量
    currentFilter: 'all' // 当前筛选条件: all, upcoming, completed
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // 设置当前月份
    const now = new Date()
    const currentMonth = `${now.getFullYear()}年${now.getMonth() + 1}月`
    this.setData({
      currentMonth
    })
    
    // 加载训练日程数据
    this.loadTrainingSchedules()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 页面显示时可以重新加载数据
    if (!this.data.loading) {
      this.loadTrainingSchedules()
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 下拉刷新时，重置页码并重新加载数据
    this.setData({
      pageNum: 1,
      hasMore: true
    })
    this.loadTrainingSchedules(true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 上拉触底时，如果还有更多数据，加载下一页
    if (this.data.hasMore && !this.data.loading) {
      this.loadTrainingSchedules()
    }
  },

  /**
   * 加载训练日程数据
   * @param refresh 是否是刷新操作
   */
  loadTrainingSchedules(refresh: boolean = false) {
    this.setData({
      loading: true
    })

    // 模拟API请求
    // 实际项目中应该使用request工具请求真实接口
    setTimeout(() => {
      // 模拟训练日程数据
      const mockTrainingSchedules: TrainingSchedule[] = [
        {
          id: '1',
          date: '2023-11-15',
          weekday: '周三',
          title: '基础技术训练',
          startTime: '18:00',
          endTime: '20:00',
          location: '主体育馆',
          coach: '张教练',
          participants: 12,
          status: 'upcoming',
          description: '重点练习传球和垫球技术'
        },
        {
          id: '2',
          date: '2023-11-17',
          weekday: '周五',
          title: '战术配合训练',
          startTime: '19:00',
          endTime: '21:00',
          location: '副体育馆',
          coach: '李教练',
          participants: 15,
          status: 'upcoming',
          description: '主攻与二传配合练习'
        },
        {
          id: '3',
          date: '2023-11-19',
          weekday: '周日',
          title: '实战对抗训练',
          startTime: '16:00',
          endTime: '18:30',
          location: '主体育馆',
          coach: '王教练',
          participants: 20,
          status: 'upcoming',
          description: '队内分组对抗赛'
        },
        {
          id: '4',
          date: '2023-11-10',
          weekday: '周五',
          title: '体能训练',
          startTime: '17:30',
          endTime: '19:30',
          location: '健身房',
          coach: '陈教练',
          participants: 8,
          status: 'completed',
          description: '力量和耐力训练'
        },
        {
          id: '5',
          date: '2023-11-12',
          weekday: '周日',
          title: '发球专项训练',
          startTime: '15:00',
          endTime: '17:00',
          location: '主体育馆',
          coach: '张教练',
          participants: 10,
          status: 'completed',
          description: '跳发球和飘球技术练习'
        }
      ]

      let newData = [...this.data.trainingSchedules]
      if (refresh) {
        newData = mockTrainingSchedules
      } else {
        newData = [...newData, ...mockTrainingSchedules]
      }

      // 计算统计数据
      const upcomingCount = newData.filter(item => item.status === 'upcoming').length
      const completedCount = newData.filter(item => item.status === 'completed').length
      const totalCount = newData.length

      this.setData({
        trainingSchedules: newData,
        loading: false,
        pageNum: this.data.pageNum + 1,
        hasMore: this.data.pageNum < 3, // 模拟只有3页数据
        upcomingCount,
        completedCount,
        totalCount
      })

      // 停止下拉刷新动画
      if (refresh) {
        wx.stopPullDownRefresh()
      }
    }, 1000)
  },

  /**
   * 筛选训练日程
   */
  onFilterChange(e: WechatMiniprogram.TouchEvent) {
    const { filter } = e.currentTarget.dataset
    this.setData({
      currentFilter: filter
    })
    // 实际项目中应该根据筛选条件重新加载数据
  },



  /**
   * 查看总结
   */
  onViewSummary(e: WechatMiniprogram.TouchEvent) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/subpackage_volleyball/training_summary/training_summary?id=${id}`
    })
  },

  /**
   * 切换到上个月
   */
  onPrevMonth() {
    const [year, month] = this.data.currentMonth.split('年')
    let newYear = parseInt(year)
    let newMonth = parseInt(month.replace('月', '')) - 1
    
    if (newMonth < 1) {
      newMonth = 12
      newYear -= 1
    }
    
    this.setData({
      currentMonth: `${newYear}年${newMonth}月`,
      pageNum: 1,
      hasMore: true
    })
    
    this.loadTrainingSchedules()
  },

  /**
   * 切换到下个月
   */
  onNextMonth() {
    const [year, month] = this.data.currentMonth.split('年')
    let newYear = parseInt(year)
    let newMonth = parseInt(month.replace('月', '')) + 1
    
    if (newMonth > 12) {
      newMonth = 1
      newYear += 1
    }
    
    this.setData({
      currentMonth: `${newYear}年${newMonth}月`,
      pageNum: 1,
      hasMore: true
    })
    
    this.loadTrainingSchedules()
  },

  /**
   * 查看训练详情
   */
  onViewTrainingDetail(e: WechatMiniprogram.TouchEvent) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/subpackage_volleyball/training_detail/training_detail?id=${id}`
    })
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '排球训练安排',
      path: '/subpackage_volleyball/training/training',
      imageUrl: '/images/volleyball-icon.svg'
    }
  }
})